/**
 * Copyright 2011 FeedDreamwork SIG
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.feeddreamwork;

import java.util.*;

import javax.jdo.annotations.*;

import com.google.appengine.api.users.*;

@PersistenceCapable
public class FeedInfo {
	@PrimaryKey
	@Persistent
	private String feedId;
	@Persistent
	private int frequency;
	@Persistent
	private String targetType;
	@Persistent
	private List<String> processors;
	@Persistent
	private User owner;

	public FeedInfo(String feedId, int frequency, String targetType, User owner) {
		this(feedId, frequency, targetType, owner, null);
	}

	public FeedInfo(String feedId, int frequency, String targetType,
			User owner, List<String> processors) {
		if (processors == null)
			processors = new ArrayList<String>();
		Utils.throwIfNullOrEmpty(feedId);

		this.feedId = feedId;
		setFrequency(frequency);
		setTargetType(targetType);
		setProcessors(processors);
		setOwner(owner);
	}

	public String getFeedId() {
		return feedId;
	}

	public int getFrequency() {
		return frequency;
	}

	public void setFrequency(int frequency) {
		Utils.throwIfSmallerThan(frequency, ApplicationProperty.MIN_FREQUENCY);
		this.frequency = frequency;
	}

	public List<String> getProcessors() {
		return processors;
	}

	public void setProcessors(List<String> processors) {
		Utils.throwIfNull(processors);
		this.processors = processors;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		Utils.throwIfNullOrEmpty(targetType);
		this.targetType = targetType;
	}

	public User getOwner() {
		return owner;
	}

	public void setOwner(User owner) {
		Utils.throwIfNull(owner);
		this.owner = owner;
	}

	public boolean hasModifyPermission(UserService userService) {
		Utils.throwIfNull(userService);

		User user = userService.getCurrentUser();
		if (!userService.isUserLoggedIn())
			return false;
		if (userService.isUserAdmin() || owner.equals(user))
			return true;
		else
			return false;
	}
}
